package com.tang.auth.domain.service.impl;

import com.alibaba.fastjson2.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tang.auth.domain.convert.AuthPermissionBOConverter;
import com.tang.auth.domain.entity.AuthPermissionBO;
import com.tang.auth.domain.service.AuthPermissionDomainService;
import com.tang.auth.infra.basic.entity.AuthPermission;
import com.tang.auth.infra.basic.service.AuthPermissionService;
import com.tang.common.enums.DeleteEnum;
import com.tang.common.enums.ShowEnum;
import com.tang.common.redis.AuthKeyBuilder;
import com.tang.component.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Resource
    private AuthPermissionService authPermissionService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 添加权限
     *
     * @param authPermissionBO 添加权限
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/15 14:25
     **/
    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthPermissionDomainServiceImpl.add.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter.INSTANCE.convertBOToEntity(authPermissionBO);
        authPermission.setIsDeleted(DeleteEnum.UN_DELETED.getCode());
        authPermissionService.insert(authPermission);
        return true;
    }

    /**
     * 是否展示权限
     *
     * @param authPermissionBO 待更改的权限内容
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/15 14:30
     **/
    @Override
    public Boolean ifShow(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthPermissionDomainServiceImpl.delete.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter.INSTANCE.convertBOToEntity(authPermissionBO);
        authPermission.setIfShow(ShowEnum.SHOW.getCode());
        authPermissionService.update(authPermission);
        return true;
    }

    /**
     * 更新权限信息
     *
     * @param authPermissionBO 权限信息
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/15 14:28
     **/
    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthPermissionDomainServiceImpl.update.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter.INSTANCE.convertBOToEntity(authPermissionBO);
        authPermissionService.update(authPermission);
        return true;
    }

    /**
     * 删除权限
     *
     * @param authPermissionBO 待删除的权限
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/15 14:28
     **/
    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthPermissionDomainServiceImpl.delete.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter.INSTANCE.convertBOToEntity(authPermissionBO);
        authPermission.setIsDeleted(DeleteEnum.DELETE.getCode());
        authPermissionService.update(authPermission);
        return true;
    }

    /**
     * 查询用户权限
     *
     * @param userName 用户名
     * @return java.util.List<java.lang.String>
     * @author Tang
     * @date 2024/2/18 16:24
     **/
    @Override
    public List<String> getPermissionList(String userName) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthPermissionDomainServiceImpl.getPermissionList.bo:{}", userName);
        }
        String permissionKey = AuthKeyBuilder.getPermissionKey(userName);
        String permissions = redisUtil.get(permissionKey);
        if (StringUtils.isBlank(permissions)) {
            return Collections.emptyList();
        }
        //将json数据转为List
        Type type = new TypeToken<List<String>>() {
        }.getType();
        return new Gson().fromJson(permissions, type);
    }
}
